package cn.b.training.baby_education_training.service.impl;

import cn.b.training.baby_education_training.entity.BabyInfo;
import cn.b.training.baby_education_training.entity.BabyInoculate;
import cn.b.training.baby_education_training.entity.Vaccine;
import cn.b.training.baby_education_training.repository.BabyInfoRepository;
import cn.b.training.baby_education_training.repository.BabyInoculateRepository;
import cn.b.training.baby_education_training.repository.VaccineRepository;
import cn.b.training.baby_education_training.service.BabyInoculateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @program: baby_education_training
 * @description
 * @author: wuying
 * @create: 2022-12-04 18:15
 **/
@Service
public class BabyInoculateServiceImpl implements BabyInoculateService {
    @Autowired
    private BabyInfoRepository babyInfoRepository;
    @Autowired
    private VaccineRepository vaccineRepository;
    @Autowired
    private BabyInoculateRepository babyInoculateRepository;
    @Override
    public boolean addInoculate(BabyInoculate babyInoculate) throws Exception {
        BabyInfo babyInfo = babyInfoRepository.getByBabyId(babyInoculate.getBabyId());
        Vaccine vaccine = vaccineRepository.getVaccineById(babyInoculate.getVaccineId());
        if (babyInfo!=null&&vaccine!=null){
            babyInoculate.setBabyId(babyInfo.getId());
            babyInoculate.setVaccineId(vaccine.getId());
            babyInoculate.setVaccineTime(LocalDateTime.now());
            babyInoculate = babyInoculateRepository.save(babyInoculate);
        }else {
            throw new Exception("信息不存在");
        }
        if (babyInoculate!=null){
            return true;
        }
        return false;
    }

    @Override
    public List<BabyInoculate> getInoculateByBabyId(Long babyId) {
        List<BabyInoculate> inoculates = babyInoculateRepository.getInoculatesByBabyId(babyId);
        return inoculates;
    }

    @Override
    public List<BabyInoculate> getInoculateByBabyIdAndVaccineId(Long babyId, Long vaccineId) {
        List<BabyInoculate> inoculates = babyInoculateRepository.getInoculatesByBabyIdAndVaccineId(babyId,vaccineId);
        return inoculates;
    }

    @Override
    public Boolean deleteInoculateById(Long id) {
        try{
            this.babyInoculateRepository.deleteById(id);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }
}
